package com.qf.http;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/20
 */
public class SocketChannelHandlerHttpReq extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest req) throws Exception {
        System.out.println(req.uri());
        System.out.println(req.method().name());
        HttpHeaders headers = req.headers();
        List<Map.Entry<String, String>> entries = headers.entries();
        for (Map.Entry<String, String> entry:entries){
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("--"+key+":"+value);
        }
        //响应给浏览器
        Channel channel = channelHandlerContext.channel();

        FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK);

        //把html内容写入到resp对象里面
        String html = "<html><body><h1>hello world</h1></body></html>";
        resp.content().writeBytes(html.getBytes("utf-8"));

        //输出resp对象
        channel.writeAndFlush(resp);
    }
}
